package com.globallogic.javatrainings.sergiiivanov.tests;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.junit.Test;

import com.globallogic.javatrainings.sergiiivanov.model.Const;
import com.globallogic.javatrainings.sergiiivanov.model.MyArrayToNamesCovertable;
import com.globallogic.javatrainings.sergiiivanov.model.Person;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityWarning;
import com.globallogic.javatrainings.sergiiivanov.resources.ErrorCodes;

public abstract class BasicPersonTest extends BasicTest {
    Person person;
    final Const.Discipline CPP = Const.Discipline.Cpp;
    Date tdate;
    Date tdate2;
    final String temail = "come@and.say.it";
    final String temail2 = "gone@and.lost";
    final String tname = "Ivan Draga";
    final String tname2 = "Lola Kitty";
    final String tphone = "066 666 66 66";
    final String tphone2 = "170 090 60 90";

    public void BasicPersonSetup() throws UniversityWarning {
        person.setName(name);
        person.setMobile(mobile);
        person.setEmail(email);
        Calendar c = GregorianCalendar.getInstance();
        c.set(2012, 12, 16);
        tdate = c.getTime();
        c.set(2012, 6, 16);
        tdate2 = c.getTime();
    }
    
    @Test
    public void setFieldsTest() throws UniversityWarning {
        assertTrue("name", name.equals(person.getName()));
        assertTrue("phone", mobile.equals(person.getMobile()));
        assertTrue("email", email.equals(person.getEmail()));
        person.setName(tname2);
        person.setMobile(tphone2);
        person.setEmail(temail2);
        assertTrue("name update", tname2.equals(person.getName()));
        assertTrue("phone update", tphone2.equals(person.getMobile()));
        assertTrue("email update", temail2.equals(person.getEmail()));
    }

    @Test
    public void badNameTest() {
        try {
            person.setName("");
            fail("Missed " + ErrorCodes.PERSON_NAME_NULL);
        } catch (Exception e) {
            assertEquals(ErrorCodes.PERSON_NAME_NULL, e.getMessage());
        }
        try {
            person.setName(null);
            fail("Missed " + ErrorCodes.PERSON_NAME_NULL);
        } catch (Exception e) {
            assertEquals(ErrorCodes.PERSON_NAME_NULL, e.getMessage());
        }
    }
    
    @Test
    public void ResumeAddTest() throws Exception {
        try {
            person.getResumeEntry(0);
            fail("Missed " + ErrorCodes.RESUME_ENTRY_NOT_FOUND_BY_INDEX + 0);
        } catch (Exception e) {
            assertEquals(ErrorCodes.RESUME_ENTRY_NOT_FOUND_BY_INDEX + 0, e.getMessage());
        }
        person.addResumeEntry("2011", "finished school", "top grade");
        assertEquals("resume adding", 1, person.countResumeEntries());
        assertArrayEquals(
                new String[] { "2011", "finished school", "top grade" },
                person.getResumeEntry(0).asArray());
        try {
            person.getResumeEntry(5);
            fail("Missed " + ErrorCodes.RESUME_ENTRY_NOT_FOUND_BY_INDEX + 5);
        } catch (Exception e) {
            assertEquals(ErrorCodes.RESUME_ENTRY_NOT_FOUND_BY_INDEX + 5, e.getMessage());
        }
    }
    
    @Test
    public void getName$MyArrayToNamesCovertableTest() {
        assertEquals(name, person.getName());
        assertEquals(name, ((MyArrayToNamesCovertable)person).getName());
    }
}
